const Sequelize = require('sequelize');
import sequelize from './db/sequelize';

/**
 * 单张图片实体
 */
const WorkNormal = sequelize.define('work_normals', {
  id                    : {type: Sequelize.BIGINT,    allowNull: false, primaryKey: true, autoIncrement: true},
  uid                   : {type: Sequelize.STRING,    allowNull: false},
  
  name                  : {type: Sequelize.STRING,    allowNull: false },
  type                  : {type: Sequelize.INTEGER,   allowNull: false  , defaultValue : -1},
  gategory              : {type: Sequelize.INTEGER,   allowNull: false  , defaultValue : -1},

  complete_time         : {type: Sequelize.INTEGER,   allowNull: true },
  complete_place        : {type: Sequelize.STRING,    allowNull: true },

  release_flag          : {type: Sequelize.INTEGER,   allowNull: true , defaultValue : 1},
  release_time          : {type: Sequelize.INTEGER,   allowNull: true },
  release_place         : {type: Sequelize.STRING,    allowNull: true },

  url                   : {type: Sequelize.STRING,    allowNull: false },
  describe              : {type: Sequelize.STRING,    allowNull: false },
  contact_id            : {type: Sequelize.STRING,   allowNull: false },
  hash_code             : {type: Sequelize.STRING,    allowNull: true , unique:'work_hash_index' , defaultValue : null},
  handle_code           : {type: Sequelize.STRING,    allowNull: false },

  status                : {type: Sequelize.INTEGER,   allowNull: false , defaultValue : 1 },
  remark                : {type: Sequelize.STRING,    allowNull: true },
  ext                   : {type: Sequelize.TEXT,      allowNull: true },
  ctime                 : {type: Sequelize.INTEGER,   allowNull: true },
  htime                 : {type: Sequelize.INTEGER,   allowNull: true }

},
{
    charset: 'utf8'
});


WorkNormal.typeCount = function (params) {
  var sql = 'select count(*) as count , type from work_normals where 1=1'
  if (params && params.uid && params.uid.length >= 1) {
        var uids = params.uid.join("','")
        sql += " and uid in ('"
        sql += uids
        sql += "')"
    }
    sql += ' group by type'
   return sequelize.query(sql, { replacements: params, type: Sequelize.QueryTypes.SELECT })
}

WorkNormal.gategoryCount = function (params) {
  var sql = 'select count(*) as count , gategory from work_normals where 1=1'
  if (params && params.uid && params.uid.length >= 1) {
        var uids = params.uid.join("','")
        sql += " and uid in ('"
        sql += uids
        sql += "')"
    }
    sql += ' group by gategory'
   return sequelize.query(sql, { replacements: params, type: Sequelize.QueryTypes.SELECT })
}


WorkNormal.sync({force: false}).then(function(){});
export default WorkNormal;